﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FirstController : MonoBehaviour, ISceneController, IUserAction
{
	public UserGUI userGUI;
	public DiskFactory diskFactory;
	public IActionManager actionManager;
	public ScoreRecorder scoreRecorder;

	private Queue<GameObject> diskQueue=new Queue<GameObject>();
	private int round = 1;
	private float shotInterval = 2F;
	private bool playing = false;
	private bool gameStart = false;
	private bool gameOver = false;
	private float time = 0;

	void Awake()
	{
		SSDirector director = SSDirector.getInstance();
		director.currentSceneController = this;
		diskFactory = Singleton<DiskFactory>.Instance;
		actionManager = Singleton<CCActionManager>.Instance;
		scoreRecorder = Singleton<ScoreRecorder>.Instance;
		userGUI = gameObject.AddComponent<UserGUI>() as UserGUI;
	}

	void Update()
    {
        if (gameStart)
        {
            if (gameOver)
            {
				CancelInvoke("LoadResources");
            }

            if (!playing)
            {
				InvokeRepeating("LoadResources",1F,shotInterval);
				playing = true;
            }

			ShotDisk();

			time += Time.deltaTime;

			if (time>=15F && round == 1)
            {
				userGUI.nextRound();
				round = 2;
				shotInterval -= 0.5F;
				CancelInvoke("LoadResources");
				playing = false;
			}
			
			if (time>=30F && round == 2)
            {
				userGUI.nextRound();
				round = 3;
				shotInterval -= 0.5F;
				CancelInvoke("LoadResources");
				playing = false;
			}

			if (time >= 45F && round == 3)
			{
				userGUI.nextRound();
			}
		}
    }

	public void LoadResources()
	{
		diskQueue.Enqueue(diskFactory.GetDisk(round));
	}

	private void ShotDisk()
    {
		float positionX = 12F;
		if (diskQueue.Count != 0)
		{
			GameObject disk = diskQueue.Dequeue();
			disk.SetActive(true);

			float ranY = Random.Range(1F, 2F);
			float ranX = Random.Range(-1F, 1F) < 0 ? -1 : 1;
			disk.GetComponent<DiskData>().direction = new Vector3(ranX, ranY * 0.5F, 0);
			Vector3 position = new Vector3(-disk.GetComponent<DiskData>().direction.x * positionX, ranY, 0);
			disk.transform.position = position;
			actionManager.DiskFly(disk);
		}
	}

	public void Hit(Vector3 mp)
    {
		Camera ca = Camera.main;

		Ray ray = ca.ScreenPointToRay(mp);

		RaycastHit[] hits = Physics.RaycastAll(ray);

		foreach (RaycastHit hit in hits)
		{
			if (hit.collider.gameObject.GetComponent<DiskData>() != null)
            {
				scoreRecorder.Record(hit.collider.gameObject);
				diskFactory.FreeDisk(hit.collider.gameObject);
			}
		}
	}

	public int GetScore()
    {
		return scoreRecorder.score;
    }

	public void Restart()
	{
		gameOver = false;
		playing = false;
		scoreRecorder.Reset();
		round = 1;
		shotInterval = 2F;
		time = 0;
	}

	public void GameOver()
    {
		gameOver = true;
	}

	public void StartGame()
    {
		gameStart = true;
    }

	public void SetFlyMode(bool mode)
    {
        if (mode)
        {
			actionManager = Singleton<PhysicsActionManager>.Instance as IActionManager;
		}
        else
        {
			actionManager = Singleton<CCActionManager>.Instance as IActionManager;
        }
	}
}